﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TwitterClient2.Model;
using System.Windows.Forms;
using NTwitter;
using System.Collections.ObjectModel;
using TwitterClient2.Components;
using Microsoft.Win32;

namespace TwitterClient2.Controller
{

    class TwitterClientController
    {
        private TwitterClientModel model = TwitterClientModel.getInstance();
        public Twitter viewmanager { set; get; }
        public const byte STARTANIMATION_FINISH = 1;
        public const byte LOGIN_CLICK = 2;
        public const byte MAINMENU_HOME_BUTTON_CLICK = 3;
        public const byte SEARCH_BUTTON_CLICK = 4;

        public const byte MAINMENU_SEARCH_BUTTON_CLICK = 5;
        public const byte USER_CLICK_HOME_OR_SEARCH = 6;
        public const byte UPDATE_STATUS_CLICK = 7;
        public const byte MAINMENU_FOLLOWERS_CLICK = 8;
        public const byte USER_CLICK_FRIENDS_FOLLOWERS = 9;
        public const byte MAINMENU_FRIENDS_BUTTON_CLICK = 10;
        public const byte MAINMENU_MESSAGES_BUTTON_CLICK = 11;
        public const byte SEND_DIRECT_MESSAGE = 12;
        public const byte GET_MESSAGE_DETAILS = 13;
        public const byte MESSAGE_DETAILS_CANCEL_CLICK = 14;
        public const byte MESSAGE_DETAILS_REPLY_CLICK = 15;
        public const byte MAINMENU_NEWS_CLICK = 16;
        public const byte UPDATE_DATA = 17;
        public const byte CHANGE_USER = 18;
        public const byte FOLLOW_USER = 19;
        public const byte UNFOLLOW_USER = 20;

        public const byte NAVIGATION_MAINMENU_CLICK = 80;
        public const byte NAVIGATION_SEARCH_USER_CLICK = 81;
        public const byte NAVIGATION_HOME_USER_CLICK = 82;
        public const byte NAVIGATION_FOLLOWERS_USER_CLICK = 83;
        public const byte NAVIGATION_FIRENDS_USER_CLICK = 84;
        public const byte NAVIGATION_NEWS_USER_CLICK = 85;
        public const byte NAVIGATION_MESSAGES_USER_CLICK = 86;

        private static TwitterClientController controller;
        private TwitterClientController()
        {

        }
        public void performUserInput(byte input, TwitterClientEvent twitterEvent)
        {
            switch (input)
            {
                case LOGIN_CLICK:

                    string message;
                    if (this.model.authentification((String)twitterEvent.paramList[0], (String)twitterEvent.paramList[1], out message))
                    {
                        if ((bool)twitterEvent.paramList[2])
                        {
                            RegistryKey userkey = Registry.CurrentUser;
                            RegistryKey softwarekey = userkey.OpenSubKey("Software", true);
                            RegistryKey mainkey = softwarekey.OpenSubKey("TwitterClient", true);
                            if (mainkey == null)
                                mainkey = softwarekey.CreateSubKey("TwitterClient");
                            mainkey.SetValue("user", this.model.username);
                            mainkey.SetValue("pw", this.model.password);


                        }

                        this.viewmanager.PanelHomeInstance.UpdateData(this.model.userTweets);
                        this.viewmanager.PanelFollowersInstance.updateData(this.model.followers);
                        this.viewmanager.PanelFriendsInstance.updateData(this.model.friends);
                        this.viewmanager.PanelMessagesInstance.updateData(this.model.messages, this.model.messagesSent);
                        this.viewmanager.PanelNewsInstance.updateData(this.model.publicTweets, this.model.friendsTweets, this.model.mentionTweets);

                        this.viewmanager.MainMenuBottonInstance.MenuItems.Add(this.viewmanager.MenuItemUpdateInstance);
                        this.viewmanager.MainMenuBottonInstance.MenuItems.Add(this.viewmanager.MenuItemChangeUserInstance);

                        this.viewmanager.PanelStartBackgroundInstance.Visible = false;
                        this.viewmanager.PanelLoginInstance.Visible = false;
                        this.viewmanager.PanelMainMenueInstance.Visible = true;
                    }
                    else
                    {
                        MessageBox.Show(message);
                        this.viewmanager.PanelLoginInstance.Enabled = true;
                    }

                    break;
                case STARTANIMATION_FINISH:
                    this.viewmanager.PanelLoginInstance.Visible = true;
                    break;
                case MAINMENU_HOME_BUTTON_CLICK:
                    this.viewmanager.PanelMainMenueInstance.Visible = false;
                    this.viewmanager.PanelHomeInstance.Visible = true;
                    break;
                case SEARCH_BUTTON_CLICK:
                    this.viewmanager.PanelSearchInstance.Enabled = false;
                    int type = (int)twitterEvent.paramList[0];
                    string searchRequest = (string)twitterEvent.paramList[1];
                    Collection<Status> results;

                    if (type == 1)
                        results = this.model.search(searchRequest);
                    else if (type == 2)
                        results = this.model.searchFromUser(searchRequest);
                    else
                        results = this.model.searchReplyToUser(searchRequest);

                    this.viewmanager.PanelSearchInstance.UpdateData(results);
                    this.viewmanager.PanelSearchInstance.Enabled = true;
                    break;
                case UPDATE_STATUS_CLICK:

                    this.model.updateStatus((string)twitterEvent.paramList[0]);
                    break;
                case NAVIGATION_MAINMENU_CLICK:
                    Panel p = (Panel)twitterEvent.paramList[0];
                    p.Visible = false;
                    this.viewmanager.PanelMainMenueInstance.Visible = true;
                    break;
                case NAVIGATION_SEARCH_USER_CLICK:
                    this.viewmanager.PanelUserInstance.Visible = false;
                    this.viewmanager.PanelSearchInstance.Visible = true;
                    break;
                case MAINMENU_SEARCH_BUTTON_CLICK:
                    this.viewmanager.PanelMainMenueInstance.Visible = false;
                    this.viewmanager.PanelSearchInstance.Visible = true;
                    break;
                case USER_CLICK_HOME_OR_SEARCH:
                    byte location = (byte)twitterEvent.paramList[1];
                    this.viewmanager.PanelUserInstance.uploadData(this.model.getUser((string)twitterEvent.paramList[0]), location);
                    this.viewmanager.PanelUserInstance.Visible = true;
                    if (location == PanelNavigation.HOME_USER)
                        this.viewmanager.PanelHomeInstance.Visible = false;
                    else if (location == PanelNavigation.SEARCH_USER)
                        this.viewmanager.PanelSearchInstance.Visible = false;
                    else if (location == PanelNavigation.NEWS_USERS)
                        this.viewmanager.PanelNewsInstance.Visible = false;
                    else if (location == PanelNavigation.MESSAGES_USERS)
                        this.viewmanager.PanelMessagesInstance.Visible = false;
                    break;
                case NAVIGATION_HOME_USER_CLICK:
                    this.viewmanager.PanelUserInstance.Visible = false;
                    this.viewmanager.PanelHomeInstance.Visible = true;
                    break;
                case MAINMENU_FOLLOWERS_CLICK:
                    this.viewmanager.PanelMainMenueInstance.Visible = false;
                    this.viewmanager.PanelFollowersInstance.Visible = true;
                    break;
                case USER_CLICK_FRIENDS_FOLLOWERS:
                    byte loc = (byte)twitterEvent.paramList[0];
                    User user = (User)twitterEvent.paramList[1];
                    this.viewmanager.PanelUserInstance.uploadData(user, loc);
                    this.viewmanager.PanelUserInstance.Visible = true;
                    if (loc == PanelNavigation.FOLLOWERS_USERS)
                        this.viewmanager.PanelFollowersInstance.Visible = false;
                    else
                        this.viewmanager.PanelFriendsInstance.Visible = false;
                    break;
                case NAVIGATION_FOLLOWERS_USER_CLICK:
                    this.viewmanager.PanelUserInstance.Visible = false;
                    this.viewmanager.PanelFollowersInstance.Visible = true;
                    break;
                case MAINMENU_FRIENDS_BUTTON_CLICK:
                    this.viewmanager.PanelMainMenueInstance.Visible = false;
                    this.viewmanager.PanelFriendsInstance.Visible = true;
                    break;
                case NAVIGATION_FIRENDS_USER_CLICK:
                    this.viewmanager.PanelUserInstance.Visible = false;
                    this.viewmanager.PanelFriendsInstance.Visible = true;
                    break;
                case MAINMENU_MESSAGES_BUTTON_CLICK:
                    this.viewmanager.PanelMessagesInstance.Visible = true;
                    this.viewmanager.PanelMainMenueInstance.Visible = false;
                    break;
                case SEND_DIRECT_MESSAGE:
                    this.viewmanager.PanelMessagesInstance.Enabled = false;
                    this.model.sendDirectMessage((string)twitterEvent.paramList[0], (string)twitterEvent.paramList[1]);
                    this.viewmanager.PanelMessagesInstance.Enabled = true;
                    break;
                case GET_MESSAGE_DETAILS:
                    this.viewmanager.PanelMessageDetailInstance.uploadData((Message)twitterEvent.paramList[0]);
                    this.viewmanager.PanelMessageDetailInstance.Visible = true;

                    break;
                case MESSAGE_DETAILS_CANCEL_CLICK:
                    this.viewmanager.PanelMessageDetailInstance.Visible = false;
                    break;
                case MESSAGE_DETAILS_REPLY_CLICK:
                    this.viewmanager.PanelMessagesInstance.tabSendMessage.textBoxMessageText.Text = "";
                    this.viewmanager.PanelMessagesInstance.tabSendMessage.textBoxSendToValue.Text = (string)twitterEvent.paramList[0];
                    this.viewmanager.PanelMessagesInstance.tabControl.SelectedIndex = 2;
                    this.viewmanager.PanelMessageDetailInstance.Visible = false;
                    break;
                case MAINMENU_NEWS_CLICK:
                    this.viewmanager.PanelNewsInstance.Visible = true;
                    this.viewmanager.PanelMainMenueInstance.Visible = false;
                    break;
                case NAVIGATION_NEWS_USER_CLICK:
                    this.viewmanager.PanelUserInstance.Visible = false;
                    this.viewmanager.PanelNewsInstance.Visible = true;
                    break;
                case NAVIGATION_MESSAGES_USER_CLICK:
                    this.viewmanager.PanelUserInstance.Visible = false;
                    this.viewmanager.PanelMessagesInstance.Visible = true;
                    break;
                case UPDATE_DATA:
                    this.model.updateData();
                    this.viewmanager.PanelHomeInstance.UpdateData(this.model.userTweets);
                    this.viewmanager.PanelFollowersInstance.updateData(this.model.followers);
                    this.viewmanager.PanelFriendsInstance.updateData(this.model.friends);
                    this.viewmanager.PanelMessagesInstance.updateData(this.model.messages, this.model.messagesSent);
                    this.viewmanager.PanelNewsInstance.updateData(this.model.publicTweets, this.model.friendsTweets, this.model.mentionTweets);
                    break;
                case CHANGE_USER:
                    this.viewmanager.PanelHomeInstance.Visible = false;
                    this.viewmanager.PanelFollowersInstance.Visible = false;
                    this.viewmanager.PanelFriendsInstance.Visible = false;
                    this.viewmanager.PanelMessagesInstance.Visible = false;
                    this.viewmanager.PanelNewsInstance.Visible = false;
                    this.viewmanager.PanelSearchInstance.Visible = false;

                    this.viewmanager.PanelMainMenueInstance.Visible = false;

                    this.viewmanager.MainMenuBottonInstance.MenuItems.Remove(this.viewmanager.MenuItemUpdateInstance);
                    this.viewmanager.MainMenuBottonInstance.MenuItems.Remove(this.viewmanager.MenuItemChangeUserInstance);

                    this.viewmanager.PanelStartBackgroundInstance.Visible = true;
                    this.viewmanager.PanelLoginInstance.Visible = true;

                    break;
                case FOLLOW_USER:
                    this.model.addFriend((string)twitterEvent.paramList[0]);
                    this.viewmanager.PanelFriendsInstance.updateData(this.model.friends);
                    break;
                case UNFOLLOW_USER:
                    this.model.removeFriend((string)twitterEvent.paramList[0]);
                    this.viewmanager.PanelFriendsInstance.updateData(this.model.friends);
                    break;
                default:
                    break;

            }

        }

        public static TwitterClientController getInstance()
        {
            if (controller == null)
                controller = new TwitterClientController();
            return controller;
        }

    }
}
